import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;


public class Sun extends PhysicsObject {
  
  public Sun(float size, boolean pickable) {
    Triangle t1 = new Triangle(size, false);
    addChild(t1);
    for (int i = 1; i <= 23; i++) {
      t1 = new Triangle(size, false);
      t1.orientation = 15 * i;
      addChild(t1);
    }
    Circle c = new Circle(size * .9f);
    addChild(c);
    
    centerOfMass.x = centerOfMass.y = size / 2;
    inverseMomentOfInertia = (float) (1 / (Math.PI * (size * .5) / 4));
    renderable = new Renderable(pickable);
    renderable.scale = size;
    renderable.CoMX = centerOfMass.x;
    renderable.CoMY = centerOfMass.y;
    inverseMass = 2 / size;
  }
  
  private class Renderable extends SceneGraphNode {
    public Renderable(boolean pickable) {
      super(pickable);
      physics = Sun.this;
    }
    public void renderGeometry(GLAutoDrawable drawable) {
      GL2 gl = drawable.getGL().getGL2();
      new Triangle(1f, false).renderable.render(drawable);
      for (int i = 0; i < 23; i++) {
        gl.glRotatef(15, 0, 0, 1);
        new Triangle(1f, false).renderable.render(drawable);
      }
      new Circle(.9f).renderable.render(drawable);
    }
  }
  
  //override PhysicsObject's updateState method to propagate changes to children
  public void updateState(float timePeriod) {
    //update square
    super.updateState(timePeriod);
    //update children
    for (PhysicsObject child : children) {
      child.updateState(timePeriod);
    }
  }
  
  public static CollisionInfo getCollision(HalfSpace a, Sun b) {
    for (int i = 0; i < 24; i++) {
      if (Triangle.getCollision((HalfSpace) a, (Triangle) b.children.get(i)) != null) {
        return Triangle.getCollision((HalfSpace) a, (Triangle) b.children.get(i));
      }
    }
    return null;
  }
}
